package com.eralink.unionuhfreader.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.eralink.unionuhfreader.R;
import com.eralink.unionuhfreader.bean.EPCBean;

import java.util.List;

public class MultiBankAutoReadTagListViewAdapter extends BaseAdapter {
    private final List<EPCBean> epcBeanList;
    private final LayoutInflater layoutInflater;

    private boolean hide_reserve_column = false;
    private boolean hide_tid_column = false;
    private boolean hide_user_column = false;

    public MultiBankAutoReadTagListViewAdapter(Context context, List<EPCBean> epcBeanList) {
        this.layoutInflater = LayoutInflater.from(context);
        this.epcBeanList = epcBeanList;
    }

    @Override
    public int getCount() {
        return epcBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return epcBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setHideReserveColumn(boolean hidden) {
        this.hide_reserve_column = hidden;
    }

    public void setHideTidColumn(boolean hidden) {
        this.hide_tid_column = hidden;
    }

    public void setHideUserColumn(boolean hidden) {
        this.hide_user_column = hidden;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        TagListViewItem tagListViewItem;
        if (view == null){
            view = layoutInflater.inflate(R.layout.multi_bank_auto_read_tag_item,parent,false);
            tagListViewItem = new TagListViewItem();
            tagListViewItem.tvIndex = view.findViewById(R.id.tv_number);
            tagListViewItem.tvReaderId = view.findViewById(R.id.tv_reader_id);
            tagListViewItem.tvPC = view.findViewById(R.id.tv_pc);
            tagListViewItem.tvEPC = view.findViewById(R.id.tv_epc);
            tagListViewItem.tvReserve = view.findViewById(R.id.tv_reserve);
            tagListViewItem.tvTid = view.findViewById(R.id.tv_tid);
            tagListViewItem.tvUser = view.findViewById(R.id.tv_user);
            tagListViewItem.tvCount = view.findViewById(R.id.tv_count);
            tagListViewItem.tvAnt = view.findViewById(R.id.tv_ant);
            view.setTag(tagListViewItem);
        }
        else {
            tagListViewItem = (TagListViewItem) view.getTag();
        }

        tagListViewItem.tvIndex.setText(String.valueOf(epcBeanList.get(position).getIndex() + 1));
        tagListViewItem.tvReaderId.setText(String.valueOf(epcBeanList.get(position).getReaderId()));
        tagListViewItem.tvPC.setText(epcBeanList.get(position).getPC());
        tagListViewItem.tvEPC.setText(epcBeanList.get(position).getEPC());
        tagListViewItem.tvReserve.setText(epcBeanList.get(position).getReserve());
        tagListViewItem.tvTid.setText(epcBeanList.get(position).getTid());
        tagListViewItem.tvUser.setText(epcBeanList.get(position).getUser());
        tagListViewItem.tvAnt.setText(String.valueOf(epcBeanList.get(position).getAnt() + 1));
        tagListViewItem.tvCount.setText(String.valueOf(epcBeanList.get(position).getCount()));

        tagListViewItem.tvReserve.setVisibility(hide_reserve_column ? View.GONE : View.VISIBLE);
        tagListViewItem.tvTid.setVisibility(hide_tid_column ? View.GONE : View.VISIBLE);
        tagListViewItem.tvUser.setVisibility(hide_user_column ? View.GONE : View.VISIBLE);

        return view;
    }

    static class TagListViewItem {
        public TextView tvIndex;
        public TextView tvReaderId;
        public TextView tvPC;
        public TextView tvEPC;
        public TextView tvReserve;
        public TextView tvTid;
        public TextView tvUser;
        public TextView tvAnt;
        public TextView tvCount;
    }
}
